//
//  DropListView.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 15/12/23.
//  Copyright © 2015年 zhangpei. All rights reserved.
//

#import "DropListView.h"
#import "UtilityConfig.h"
#import "ViewHelper.h"
#import "PublicMethod.h"

#define TABLEWIDTH   120

#define TABLEROWHEIHT   44


@interface DropListView ()
<UITableViewDataSource,UITableViewDelegate>
{
    UIWindow *window;
    UIViewController *controller;
}

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) UIImageView *shadowImageView;

@property (nonatomic, strong) NSArray *datas;

@end


static DropListView *dropListView;

@implementation DropListView
@synthesize dropListViewBlock;

@synthesize table, shadowImageView, datas;

+ (id)sharedDropListViewWithController:(UIViewController *)controller withPoint:(CGFloat)point withItems:(NSArray *)items  withDropListViewBlock:(DropListViewBlock)block
{
    @synchronized (self)
    {
        //为了确保多线程情况下，仍然确保实体的唯一性
        if (dropListView == nil)
        {
            dropListView = [[DropListView alloc] init];
        }
        
        dropListView.dropListViewBlock = [block copy];
        
        dropListView.datas = items;
        
        [dropListView.table reloadData];
        
        CGRect rect_animate_start =  CGRectMake(point>0?point:(dropListView.bounds.size.width + point - TABLEWIDTH), controller.navigationController.navigationBar.frame.size.height + STATUSBARFRAME.size.height, TABLEWIDTH, 0);
        
        CGRect rect_animate_end = CGRectMake(point>0?point:(dropListView.bounds.size.width + point - TABLEWIDTH), controller.navigationController.navigationBar.frame.size.height + STATUSBARFRAME.size.height, TABLEWIDTH, items.count *TABLEROWHEIHT);
        
        dropListView.hidden = NO;
        
        dropListView.table.frame = rect_animate_start;
        
        dropListView.shadowImageView.frame = rect_animate_end;

        [dropListView setShadow];
        
        dropListView.shadowImageView.hidden = YES;

        [WINDOW addSubview:dropListView];

        [UIView animateWithDuration:0.35 animations:^{
            dropListView.table.frame =  rect_animate_end;
        }
        completion:^(BOOL finished)
        {
            dropListView.shadowImageView.hidden = NO;
        }];

        return dropListView;
    }
}


+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (!dropListView)
        {
            dropListView = [super allocWithZone:zone]; //确保使用同一块内存地址
            
            return dropListView;
        }
        return nil;
    }
}

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        self.hidden = YES;
        
        [self initView];
    }
    return self;
}


- (void)initView
{
    window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    self.frame = window.bounds;
    [window addSubview:self];
    
    //透明覆层
    UIImageView *clearImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    clearImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:clearImageView];
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    
    table.layer.borderWidth = 0;
    
    UIColor *color = [UIColor colorWithRed:61.0f/255.0f green:159.0f/255.0f blue:234.0f/255.0f alpha:1];
    
//    table.layer.borderColor = color.CGColor;
    table.backgroundColor = color;
    
    table.separatorColor = COLORRGBA(53, 146, 224, 1);
    
    table.showsHorizontalScrollIndicator = NO;
    
    table.showsVerticalScrollIndicator = NO;
    
    table.dataSource = self;
    
    table.delegate = self;
    
    table.scrollEnabled = NO;
    
    [self addSubview:table];
    
    //隐藏多余的分割线
    [PublicMethod setExtraCellLineHidden:table];
    
    //分割线左端顶头
    if ([table respondsToSelector:@selector(setSeparatorInset:)])
    {
        [table setSeparatorInset:UIEdgeInsetsZero];
    }
    
    shadowImageView = [[UIImageView alloc] initWithFrame:table.frame];
    shadowImageView.backgroundColor = [UIColor clearColor];

    [self addSubview:shadowImageView];
    
    [self bringSubviewToFront:table];

}

- (void)setShadow
{
    shadowImageView.layer.shadowColor = COLORRGBA(50, 50, 50, 1).CGColor;//shadowColor阴影颜色
    shadowImageView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    shadowImageView.layer.shadowOpacity = 0.6;//阴影透明度，默认0
    shadowImageView.layer.shadowRadius = 1;//阴影半径，默认3
    
    //路径阴影
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    float width = shadowImageView.bounds.size.width;
    float height = shadowImageView.bounds.size.height;
    float x = shadowImageView.bounds.origin.x;
    float y = shadowImageView.bounds.origin.y;
    float addWH = 1;
    
    CGPoint topLeft      = shadowImageView.bounds.origin;
    CGPoint topMiddle = CGPointMake(x+(width/2),y-addWH);
    CGPoint topRight     = CGPointMake(x+width,y);
    
    CGPoint rightMiddle = CGPointMake(x+width+addWH,y+(height/2));
    
    CGPoint bottomRight  = CGPointMake(x+width,y+height);
    CGPoint bottomMiddle = CGPointMake(x+(width/2),y+height+addWH);
    CGPoint bottomLeft   = CGPointMake(x,y+height);
    
    
    CGPoint leftMiddle = CGPointMake(x-addWH,y+(height/2));
    
    [path moveToPoint:topLeft];
    //添加四个二元曲线
    [path addQuadCurveToPoint:topRight controlPoint:topMiddle];
    [path addQuadCurveToPoint:bottomRight controlPoint:rightMiddle];
    [path addQuadCurveToPoint:bottomLeft controlPoint:bottomMiddle];
    [path addQuadCurveToPoint:topLeft controlPoint:leftMiddle];
    //设置阴影路径
    shadowImageView.layer.shadowPath = path.CGPath;
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"TABLECELL";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        //分割线左端顶头
        if ([cell respondsToSelector:@selector(setLayoutMargins:)])
        {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        
        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
        {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
        //设置背景图片
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = [datas objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dropListViewBlock)
    {
        dropListViewBlock([datas objectAtIndex:indexPath.row]);
    }
    
    [self hiddenDropListView];
}

- (void)hiddenDropListView
{
    CGRect rect = self.table.frame;
    
    self.shadowImageView.hidden = YES;
    
    rect.size.height = 0;
    
    [UIView animateWithDuration:0.35 animations:^
    {
        self.table.frame = rect;
    }
    completion:^(BOOL finished)
    {
        self.hidden = YES;
    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenDropListView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TABLEROWHEIHT;
}

@end
